package com.whj.wkpt.service;

import com.whj.wkpt.entity.DeliveryAttachment;
import com.whj.wkpt.entity.TaskApplication;
import com.whj.wkpt.mapper.TaskApplicationMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class TaskApplicationService {

    @Autowired
    private TaskApplicationMapper taskApplicationMapper;
    
    @Autowired
    private DeliveryAttachmentService deliveryAttachmentService;

    public TaskApplication createApplication(TaskApplication taskApplication) {
        taskApplication.setApplyTime(LocalDateTime.now());
        taskApplication.setCreateTime(LocalDateTime.now());
        taskApplication.setUpdateTime(LocalDateTime.now());
        taskApplicationMapper.insert(taskApplication);
        return taskApplication;
    }

    public TaskApplication updateApplication(TaskApplication taskApplication) {
        taskApplication.setUpdateTime(LocalDateTime.now());
        taskApplicationMapper.update(taskApplication);
        return taskApplication;
    }

    public TaskApplication findById(Long applicationId) {
        return taskApplicationMapper.findById(applicationId);
    }

    public List<TaskApplication> findAllApplications() {
        return taskApplicationMapper.findAll();
    }

    public List<TaskApplication> findByTaskId(Long taskId) {
        return taskApplicationMapper.findByTaskId(taskId);
    }

    public List<TaskApplication> findByFreelancerId(Long freelancerId) {
        return taskApplicationMapper.findByFreelancerId(freelancerId);
    }

    public List<TaskApplication> findByDeliveryStatus(String deliveryStatus) {
        return taskApplicationMapper.findByDeliveryStatus(deliveryStatus);
    }

    public List<TaskApplication> findByTaskIdAndDeliveryStatus(Long taskId, String deliveryStatus) {
        return taskApplicationMapper.findByTaskIdAndDeliveryStatus(taskId, deliveryStatus);
    }

    public List<TaskApplication> searchApplications(Long taskId, String deliveryStatus) {
        if (taskId != null && deliveryStatus != null && !deliveryStatus.isEmpty()) {
            return taskApplicationMapper.findByTaskIdAndDeliveryStatus(taskId, deliveryStatus);
        } else if (taskId != null) {
            return taskApplicationMapper.findByTaskId(taskId);
        } else if (deliveryStatus != null && !deliveryStatus.isEmpty()) {
            return taskApplicationMapper.findByDeliveryStatus(deliveryStatus);
        } else {
            return taskApplicationMapper.findAll();
        }
    }
    
    public List<TaskApplication> findByTaskIds(List<Long> taskIds) {
        // 处理空列表情况，避免SQL语法错误
        if (taskIds == null || taskIds.isEmpty()) {
            return new ArrayList<>();
        }
        return taskApplicationMapper.findByTaskIds(taskIds);
    }

    public void deleteApplication(Long applicationId) {
        // 先删除与该申请关联的所有附件（解决外键约束问题）
        List<DeliveryAttachment> attachments = deliveryAttachmentService.findByApplicationId(applicationId);
        for (DeliveryAttachment attachment : attachments) {
            deliveryAttachmentService.deleteAttachment(attachment.getAttachmentId());
        }
        
        // 然后删除申请记录
        taskApplicationMapper.deleteById(applicationId);
    }
}